<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CMLLogin extends CI_Controller {
	
	 public function __construct()
	    {
	        parent::__construct();
	        session_start();
	        //$this->output->enable_profiler(FALSE);
	    }
	    
	public function index()
	{
	    if(isset($_SESSION['uid']) && $_SESSION['uid'] > 0){
	       redirect('cmladmin','refresh');
        }
        if ($this->input->post('username')){
			$u = $this->input->post('username');
			$pw = $this->input->post('password');
			$this->MUser->verifyUser($u,$pw);
			if ($_SESSION['uid'] > 0){
                if($_SESSION['role_id'] == 1)
                {
                    redirect('cmladmin','refresh');    
                }
				redirect('pagelist','refresh');
			}
		}
        $data['title'] = "Administrator Login";
		$this->load->vars($data);
		$this->load->view('admin/login'); 
	}
	
	public function setting() {
		if (!isset($_SESSION['uid']) || $_SESSION['uid'] < 1){
		   	redirect('memberlogin/requiredmessage','refresh');
		}
		
		if ($this->input->post('submit')){
	  		if($_SESSION['role_id']==1) {	
                $this->MStaff->updateStaff();
                $this->session->set_flashdata('message','Account updated');
	  			redirect('hradmin','refresh');
			}
	  		elseif($_SESSION['role_id']==2) {	
	  		    $this->MStaff->updateAccountSetting();
                $this->session->set_flashdata('message','Account updated');
				redirect('employee','refresh');
			}
	  		
	  	}else{
			$data['title'] = "Account Setting";
			$data['main'] = 'account_form_setting';
			$data['roles'] = $this->MUser->getRoles();
			$data['status'] = $this->MStaff->getStatusDropdown();
			$data['positions'] = $this->MPosition->getPositionDropdown();	
			$data['teams'] = $this->MTeam->getTeamDropdown();
			$data['staff'] = $this->MStaff->getAccountDetailByUId($_SESSION['uid']);	
        	if($_SESSION['role_id']==1) {			
                $data['locations'] = $this->MStaff->getLocationDropDown();
                $data['lastSID'] = $this->MStaff->getLastSID(0,1);
  			    $data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/hradmin');
			}
	  		elseif($_SESSION['role_id']==2) {	
				$data['form'] = $this->MFormGlobal->getFormSaveCancelBottom('/index.php/employee');
			}
			$this->load->vars($data);
			$this->load->view('template');   
		}
	}
	
	function logout(){
		unset($_SESSION['uid']);
		unset($_SESSION['role_id']);
		unset($_SESSION['username']);
		$this->session->set_flashdata('logout-alert',"You've been logged out!");
		redirect('cmladministration','refresh');
	 }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */